package com.prs.crm.action.sys;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.sys.Log;
import com.prs.crm.domain.sys.Module;
import com.prs.crm.log.ILogger;
import com.prs.crm.service.sys.LogService;

public class LogAction extends BaseAction implements ServletRequestAware{

	private static final long serialVersionUID = 1L;
	
	private LogService logService;
	
	private HttpServletRequest servletRequest;
	
	private ILogger logger;
	
	private Log log;
	
	private Integer logid;
	
	private PaginationSupport pagination;
	
	private Module module;
	
	private Integer[] logIds;
	

	


	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public LogService getLogService() {
		return logService;
	}
	
    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

	public void setLogger(ILogger logger) {
		this.logger = logger;
	}

	public ILogger getLogger() {
		return logger;
	}



	public void setLog(Log log) {
		this.log = log;
	}

	public Log getLog() {
		return log;
	}

	public void setLogid(Integer logid) {
		this.logid = logid;
	}

	public Integer getLogid() {
		return logid;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Module getModule() {
		return module;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}



	public void setLogIds(Integer[] logIds) {
		this.logIds = logIds;
	}

	public Integer[] getLogIds() {
		return logIds;
	}
	
	@Action("listMyLog")
	public String listMyLog(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Log.class)
		.add(Restrictions.eq("operator.id", AppContext.getAccountContext().getUser().getId()));
		criteria.addOrder(Order.desc("id"));
		this.setPagination(this.getLogService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	
	@Action("listLog")
	public String execute()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Log.class);
		criteria.addOrder(Order.desc("id"));
		this.setPagination(this.getLogService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	
	@Action(value="removeLog",results={@Result(name="success",type="redirect",location="listLog")})
	public String remove()
	{
		this.getLogService().batchRemove(logIds);
		return SUCCESS;
	}
	
	@Action(value="removeAll",results={@Result(name="success",type="redirect",location="listLog")})
	public String removeAll()
	{
		this.getLogService().delLog();
		return SUCCESS;
	}
	@Action(value="log",results={@Result(name="success",type="json",params={"root","prompt"})})
	public String log()
	{
		this.getLogger().logModule(module,
				AppContext.getAccountContext().currentUser(),Util.getRemoteIp(servletRequest));
		this.setPrompt("success");
		return SUCCESS;
	}
	
	@Action("listLogItem")
	public String listLogItem()
	{
		this.setLog(this.getLogService().get(logid));
		this.setPagination(this.getLogService().pageListLogItem(this.getPaginationHelper().prepare(pagination), logid));
		return SUCCESS;
	}

	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
